﻿using HostAssistantBLL;
using HostAssistantCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HostAssistant
{
    public partial class editdb : U1Form
    {
        string id = "";
        string name = "";
        string dbname = "";
        string type = "";
        public editdb(string _id)
        {
            id = _id;
            InitializeComponent();
        }
        private void addsites_Load(object sender, EventArgs e)
        {
            var SqlInfo = dbbll.GetSqlInfo(id);
            name = SqlInfo?.Mdbname ?? "";
            dbname = SqlInfo?.Mdbname ?? "";
            type = SqlInfo?.type ?? "";

            

            lblname.Text = SqlInfo?.Mdbname ?? "";
            lbltype.Text = SqlInfo?.type ?? "";
            lblpath.Text = SqlInfo?.Mpath ?? "";
            lbltime.Text = SqlInfo?.Mtime ?? "";
        }

        #region 返回
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 重置密码

        private void simpleButton3_Click(object sender, EventArgs e)
        {

            


                 if (type == "mysql")
            {
                updatepwd f2 = new updatepwd(id);
                f2.ShowDialog(this);
            }
            else
            {
                updatepwdms f2 = new updatepwdms(id);
                f2.ShowDialog(this);
            }


            return;





            splashScreenManager1.ShowWaitForm();
            splashScreenManager1.SetWaitFormCaption("请等待");
            splashScreenManager1.SetWaitFormDescription("加载中...");
            string password = "";
            string port = "";
            var set = Data<HostAssistantCore.setting>.GetInstance().GetModelList("select *  from setting", null);
            var mysql = set.FirstOrDefault(o => o.type == type);
            string ip = mysql?.ip ?? "";
            port = mysql?.port ?? "";
            string user = mysql?.username ?? "";
            password = mysql?.userpwd ?? "";


            if(type=="mysql")
            {
                MySqlDbHelper mdb = new MySqlDbHelper();
                mdb.Initialize(ip, "mysql", user, password, port);
                if (mdb.ConnectionTest() == null)
                {
                    string newpassword = "123456";
                    mdb.Update("use mysql");
                    if (mdb.Update("update user set password=PASSWORD('" + newpassword + "') where user='" + name + "'"))
                    {
                        mdb.Update("flush privileges");
                        SqlInfo mysqlinfo = new SqlInfo();
                        mysqlinfo.ID = ZConvert.StrToInt(id);
                        mysqlinfo.Mpass = "123456";
                        string query = "update SqlInfo set Mpass=@Mpass  where  id=@ID   ";
                        Data<SqlInfo>.GetInstance().Update(query, mysqlinfo);
                        splashScreenManager1.CloseWaitForm();
                        DevExpress.XtraEditors.XtraMessageBox.Show("成功修改密码!(123456)", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        splashScreenManager1.CloseWaitForm();
                        DevExpress.XtraEditors.XtraMessageBox.Show("密码修改失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show("数据库连接失败请检查配置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else if(type=="mssql")
            {
                SqlDbHelper.Initialize("master", user, password);
                if (SqlDbHelper.ConnectionTest())
                {
                    string newpassword = "123456@a";
                 
                    if (SqlDbHelper.ExecuteNormalSql(" EXECUTE sp_password NULL,'"+ newpassword + "','"+ dbname + "'"))
                    {
                      
                        SqlInfo mysqlinfo = new SqlInfo();
                        mysqlinfo.ID = ZConvert.StrToInt(id);
                        mysqlinfo.Mpass = newpassword;
                        string query = "update SqlInfo set Mpass=@Mpass  where  id=@ID   ";
                        Data<SqlInfo>.GetInstance().Update(query, mysqlinfo);
                        splashScreenManager1.CloseWaitForm();
                        DevExpress.XtraEditors.XtraMessageBox.Show("成功修改密码!("+ newpassword + ")", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        splashScreenManager1.CloseWaitForm();
                        DevExpress.XtraEditors.XtraMessageBox.Show("密码修改失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show("数据库连接失败请检查配置!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        
        }

        #endregion

        #region 备份数据库

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            if(type=="mysql")
            {
                mysqlbak f2 = new mysqlbak(id);
                f2.ShowDialog(this);
            }
            else
            {
                mssqlbak f2 = new mssqlbak(id);
                f2.ShowDialog(this);
            }
         

            return;


            var p = PathSetbll.GetPathSet();


                  if (p==null || string.IsNullOrWhiteSpace( p?.DbBakPath)  )
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("未设置备份路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }




            splashScreenManager1.ShowWaitForm();
            splashScreenManager1.SetWaitFormCaption("请等待");
            splashScreenManager1.SetWaitFormDescription("加载中...");
            string password = "";
            string port = "";
            var set = Data<HostAssistantCore.setting>.GetInstance().GetModelList("select *  from setting", null);
            var mysql = set.FirstOrDefault(o => o.type == "mysql");
            string ip = mysql?.ip ?? "";
            port = mysql?.port ?? "";
            string user = mysql?.username ?? "";
            password = mysql?.userpwd ?? "";
            MySqlDbHelper mdb = new MySqlDbHelper();
            mdb.Initialize(ip, "mysql", user, password, port);
            if (mdb.ConnectionTest() == null)
            {
               string s=   dbbll.EcreateTaskSchd_Mysql_Single( "", dbname, p.DbBakPath+ @"\mysql\"+ dbname + "\\");
             
                if(s!=null)
                {
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show(s, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show("备份完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

               
            }
            else
            {
                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show("未能连接上数据库,请检查数据库账号密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #endregion

        #region 还原数据库
        private void simpleButton4_Click(object sender, EventArgs e)
        {
            if (type == "mysql")
            {
                mysqlrockbak f2 = new mysqlrockbak(id);
                f2.ShowDialog(this);
            }
            else
            {
                mssqlrockbak f2 = new mssqlrockbak(id);
                f2.ShowDialog(this);

            }

            return;
        }
        #endregion
    }
}